package com.sangmo.fts.clazz.repo;

import com.sangmo.boot.framework.plugin.dataview.spi.jpa.IJpaDataViewRepo;
import com.sangmo.fts.clazz.model.Clazz;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Collection;
import java.util.List;

public interface ClazzRepo extends IJpaDataViewRepo<Clazz> {

    Clazz findByExClazzId(String exClazzId);

    @Query(value = "SELECT a.* FROM clazz a WHERE " +
            "EXISTS(SELECT 1 FROM user_clazz uc WHERE uc.user_id = :userId AND a.id = uc.clazz_id )", nativeQuery = true)
    Collection<Clazz> findByUserId(@Param("userId") Long userId);

    @Query(value = "SELECT a.* FROM clazz a LEFT JOIN examination_clazz b ON a.id = b.clazz_id LEFT JOIN examination c ON c.id = b.examination_id WHERE c.id = :examinationId", nativeQuery = true)
    Collection<Clazz> findByExaminationId(@Param("examinationId") Long examinationId);
}
